<!--
 * @Author: ChunLai
 * @Date: 2021-04-26 14:34:59
 * @LastEditTime: 2025-08-22 09:52:51
 * @Description: 职业选择弹出框
 * @FilePath: \02.bldinsure\src\components\OccupationPop.vue
-->
<template>
  <div class="my-occupation">
    <div class="work-wrap">
      <div class="title">
        <span class="cancel" @click="$emit('cancel')">取消</span>
        <span class="sure" @click="confirmWork">确定</span>
      </div>
      <!-- 搜索职业 -->
      <div class="search-wrap">
        <div
          class="normal-md"
          :class="{ active: normalBtn === 1 }"
          @click="getNormalOcuu"
        >
          常用职业
        </div>
        <div class="search-box">
          <input type="text" v-model="searchText" @input="sure" />
          <van-icon
            class="clear-icon"
            name="close"
            v-show="searchText.length > 0"
            @click="clearText"
          />
        </div>
        <van-icon class="confirm-search" name="search" @click="sure" />
      </div>
      <!-- 搜索职业列表 -->
      <template v-if="showSearch">
        <div
          style="padding: 10px; text-align: center"
          v-if="searchList.length == 0"
        >
          暂未搜索到相关职业,请重新搜索
        </div>
        <div class="search-list" v-else>
          <div
            class="search-item"
            :class="{
              'search-check': selected.value === item.value,
              forbid: disableOcuup(item.accClass || ''),
            }"
            v-for="(item, idx) in searchList"
            :key="idx"
            @click="getSearchItem(item)"
          >
            <i
              class="ftc4 ft12"
              v-if="item.accClass !== undefined && item.accClass !== ''"
            >
              【{{ `${item.accClass}${levelName || "类"}` }}】
            </i>
            {{ item.name }}
          </div>
        </div>
      </template>

      <template v-else>
        <!-- 选择职业 -->
        <div class="has-value">
          已选职业:
          <em v-for="(item, index) in hasValues" :key="item + index">{{
            levOpt - 1 === index ? item : item + ">"
          }}</em>
        </div>
        <div class="choose-wrap" v-if="occuArrayList.length > 0">
          <template v-for="(item, index) in occuArrayList">
            <div
              class="lev-box"
              :class="[item.length > 0 ? 'level' + index : '']"
              v-if="item.length > 0"
              :key="'occu' + index"
            >
              <dl class="dl-box">
                <dd v-for="ele in item" :key="ele.value">
                  <div
                    class="career-item"
                    :class="{
                      active: hasValues[index] === ele.name,
                      forbid: disableOcuup(ele.accClass || ''),
                    }"
                    @click.stop="openChild(ele, index)"
                  >
                    <div
                      class="left-title"
                      :class="[ele.child.length > 0 ? '' : 'line1']"
                    >
                      {{ ele.name }}
                    </div>
                    <span
                      class="ms-row-icon"
                      v-if="ele.child.length > 0"
                    ></span>
                    <template v-else>
                      <span
                        class="right-con ftc4 ft12"
                        v-if="ele.accClass !== undefined && ele.accClass !== ''"
                      >
                        【{{ `${ele.accClass}${levelName || "类"}` }}】
                      </span>
                    </template>
                  </div>
                </dd>
              </dl>
            </div>
          </template>
          <template v-for="(item, index) in occuArrayList">
            <div
              class="dl-shadow"
              :key="'shadow' + index"
              :class="[hasValues[index].length > 0 ? 'sd' + index : '']"
              @click.stop="classShadow(index + 1)"
              v-if="
                occuArrayList.length > index + 1 &&
                occuArrayList[index + 1].length > 0
              "
            ></div>
          </template>
        </div>
        <!-- 加载提示 -->
        <div class="choose-wrap load-css" v-else>
          <van-loading type="spinner" color="#1989fa" size="24px"
            >数据加载中...</van-loading
          >
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "OccupationPopChoose",
  props: ["utype"],
  data() {
    return {
      showSearch: false,
      occuArrayList: [],
      searchText: "", // 职业搜索关键词
      searchList: [], // 搜索结果
      selected: {},
      searchArr: [],
      options: [],
      hasValues: [], // 已选职业类名
      levOpt: 1,
      normalBtn: 0,
    };
  },
  watch: {
    searchText(val) {
      if (!val) {
        this.searchList = [];
        this.showSearch = false;
        this.normalBtn = 0;
      } else {
        this.showSearch = true;
        this.normalBtn = 0;
      }
    },
  },
  computed: {
    ...mapGetters({
      levelName: "dictionary/ocuuClassName", // 职业等级名称
      normalOcuu: "dictionary/normalOcuu",
      normalOcuuKey: "dictionary/normalOcuuKey", // 常用职业匹配key值
      occuDisLevelArr: "page/occuDisLevelArr",
      occuDisLevelMin: "page/occuDisLevelMin",
      riskRelateOccup: "page/riskRelateOccup",
    }),
  },
  created() {
    // 获取首次职业选择
    let jsonName = "workList";
    if (this.riskRelateOccup && this.riskRelateOccup.length > 0) {
      const fdObj = this.riskRelateOccup.find((ele) =>
        ele.code.includes(this.mainRiskCode)
      );
      if (fdObj && fdObj.name) {
        jsonName = fdObj.name;
      }
    }
    this.$store.dispatch("json/getJson3Config", `${jsonName}`).then((res) => {
      if (Object.keys(res).length > 0) {
        this.occuArrayList.push(res[0].child);
      }
    });
  },
  mounted() {},
  methods: {
    disableOcuup(occuClass) {
      if (occuClass == "") return false;
      let res = false;
      if (occuClass * 1 >= this.occuDisLevelMin) {
        res = true;
      }
      if (
        this.occuDisLevelArr.length > 0 &&
        this.occuDisLevelArr.includes(occuClass)
      ) {
        res = true;
      }
      return res;
    },
    classShadow(idx) {
      // console.log(idx)
      this.hasValues.splice(idx - 1);
      this.occuArrayList.splice(idx);
    },
    openChild(target, lev) {
      // console.log(target)
      this.levOpt = parseInt(lev) + 1;
      // 已选职业类名赋值
      if (this.hasValues.length > 0) {
        let temArr = this.hasValues.slice(0, parseInt(lev));
        temArr[parseInt(lev)] = target.name;
        this.hasValues = temArr;
      } else {
        this.$set(this.hasValues, parseInt(lev), target.name);
      }
      // child职业列表赋值
      if (target.child.length > 0) {
        this.$set(this.occuArrayList, parseInt(lev) + 1, target.child);
      }
      if (target.hasOwnProperty("accClass")) {
        this.selected = target;
        this.confirmWork();
      } else {
        this.selected = {};
      }
    },
    confirmWork() {
      if (!this.selected.value) {
        this.$toast("请选择职业类别");
        return;
      }
      const res = this.disableOcuup(this.selected.accClass);
      if (res && this.utype != "syr") {
        this.$toast("该职业等级不符合投保规则,请确认");
        return;
      }
      this.$emit("confirm", this.selected);
      this.$emit("cancel");
    },
    // 清除搜索内容
    clearText() {
      this.searchText = "";
      this.searchList = [];
    },
    getNormalOcuu() {
      if (this.normalBtn === 0) {
        this.searchArr = [];
        this.getList(this.occuArrayList[0], this.searchArr);
        // console.log(this.searchArr);
        let listArr = [];
        for (var i = 0; i < this.normalOcuu.length; i++) {
          let resArr = [];
          resArr = this.searchArr.filter(
            (item) => item[this.normalOcuuKey].indexOf(this.normalOcuu[i]) > -1
          );
          listArr = listArr.concat(resArr);
        }
        this.searchList = listArr;
        this.normalBtn = 1;
        this.showSearch = true;
      } else if (this.normalBtn === 1) {
        this.searchList = [];
        this.normalBtn = 0;
        this.showSearch = false;
      }
    },
    sure() {
      if (this.searchArr.length === 0) {
        this.getList(this.occuArrayList[0], this.searchArr);
      }
      if (this.searchText.trim().length === 0) return;
      this.searchList = this.searchArr.filter(
        (item) => item.name.indexOf(this.searchText) > -1
      );
      if (this.searchList.length === 0) {
        this.$toast("未能查询到您搜索的职业，请切换关键词重新搜索");
      }
    },
    getList(arr, target) {
      arr.forEach((item) => {
        if (item.child && item.child.length > 0) {
          this.getList(item.child, target);
        } else {
          target.push(item);
        }
      });
    },
    // 选择搜索的职业
    getSearchItem(obj) {
      this.selected = obj;
      this.confirmWork();
    },
  },
};
</script>

<style lang="less" scoped>
@bg: #f0f0f0;
@activeColor: #da251e;

.work-wrap {
  height: 70vh;
  display: flex;
  flex-direction: column;
  color: #969799;

  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    font-size: 16px;
    color: #969799;
    background: #f0f0f0;

    .sure {
      color: @activeColor;
    }
  }

  .search-wrap {
    border-bottom: 10px solid @bg;
    background: #ffffff;
    padding: 10px;
    line-height: 26px;
    display: flex;
    justify-content: space-between;
    font-size: 16px;
    align-items: center;

    .normal-md {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 60px;
      height: 26px;
      line-height: 1;
      margin-right: 8px;
      font-size: 12px;
      border: 1px solid #999;
      border-radius: 4px;
      background-color: #fbf8f8;

      &.active {
        border: 1px solid #158ce0;
        color: #158ce0;
      }
    }

    .search-box {
      position: relative;
      flex: 1;
      background: @bg;
      border-radius: 18px;
      padding: 5px 30px 5px 20px;

      input {
        outline: none;
        border: none;
        width: 100%;
        height: inherit;
        background: transparent;
      }

      .clear-icon {
        position: absolute;
        top: 50%;
        right: 10px;
        transform: translateY(-50%);
        margin-top: 0;
      }
    }

    .confirm-search {
      color: @activeColor;
      padding-left: 10px;
      font-size: 24px;
      margin-top: 0;
    }
  }

  .search-list {
    flex: 1;
    overflow-y: auto;
    border-bottom: 10px solid @bg;

    .search-item {
      // text-align: center;
      padding: 6px 10px;
      color: #242424;
      line-height: 1.5;
      border-bottom: 1px solid @bg;

      &.search-check {
        color: #158ce0;
      }
      &.forbid {
        color: #999;
        i {
          color: #999;
        }
      }
    }
  }

  .choose-wrap {
    position: relative;
    height: 100%;
    flex: 1;

    &.load-css {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .lev-box {
      position: absolute;
      height: 100%;
      background-color: #fff;
      overflow-y: scroll;

      &.level0 {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        z-index: 10;
      }

      &.level1 {
        position: absolute;
        left: 15%;
        top: 0;
        width: 85%;
        z-index: 20;
      }

      &.level2 {
        position: absolute;
        left: 30%;
        top: 0;
        width: 70%;
        z-index: 30;
      }

      &.level3 {
        position: absolute;
        left: 45%;
        top: 0;
        width: 55%;
        z-index: 40;
      }
    }

    .dl-shadow {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      display: none;
      background-color: rgba(216, 214, 214, 0.5);

      &.sd0 {
        display: block;
        z-index: 11;
      }

      &.sd1 {
        left: 15%;
        width: 85%;
        display: block;
        border-left: 1px solid #b7b4b4;
        z-index: 21;
      }

      &.sd2 {
        left: 30%;
        width: 70%;
        display: block;
        border-left: 1px solid #b7b4b4;
        z-index: 31;
      }
    }
  }
}

.has-value {
  padding: 6px 14px;
  line-height: 1.5;
  border-bottom: 1px dashed;

  em {
    color: #969799;
    font-style: normal;
  }

  i {
    color: #969799;
    font-style: normal;
  }
}

.career-item {
  display: flex;
  line-height: 2.5;
  font-size: 14px;
  justify-content: space-between;
  align-items: center;
  padding: 0 4px 0 10px;
  color: #000;
  border-bottom: 1px solid #f0f0f0;

  &.active {
    color: #158ce0;
  }

  .right-con {
    text-align: right;
  }
  &.forbid {
    color: #999;
    .right-con {
      color: #999;
    }
  }

  .left-title {
    width: 70%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
<style>
.my-occupation .van-popup {
  padding-top: 0 !important;
}
</style>
